<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用TP5助手函数可实现单字母函数M D U等,也可db::name方式,可双向兼容
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */

namespace app\admin\controller;
use think\AjaxPage;
use think\Page;
use think\Db;
use app\admin\logic\DistributLogic;
use think\Loader;

class Distribut extends Base {


    /**
     * 分销关系首页
     * @return mixed
     */
    public function relation(){
        return $this->fetch();
    }

    /**
     * AJAX关系列表
     */
    public function ajaxrelation(){
        // 搜索条件
        $condition = array();
        if(trim(I('keyword'))){
            $condition['relation_name'] = I('keyword');
        }
        $sort_order = I('order_by').' '.I('sort');

        $model = M('distribut_relation');
        $count = $model->where($condition)->count();
        $Page  = new AjaxPage($count,15);
        //  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
        $relationList = $model->where($condition)->order($sort_order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('relationList',$relationList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }

    /**
     * 新增关系
     * @return mixed
     */
    public function add_relation(){
        if(IS_POST){
            $data = I('post.');
            $data['admin_id'] = session('admin_id');
            $distribut_obj = new DistributLogic();
            $res = $distribut_obj->addRelation($data);
            if($res['status'] == 1){
                $this->success('添加成功',U('Distribut/relation'));
            }else{
                $this->error('添加失败,'.$res['msg'],U('Distribut/relation'));
            }
        }
        return $this->fetch();
    }


    /**
     * 修改关系
     */
    public function edit_relation(){
        $id = I('id/d',0);
        $relationRow =  M('distribut_relation')->where(['id'=>$id])->find();
        if(!$relationRow){
            $this->error('非法请求,关系不存在',U('Distribut/relation'));
        }
        if(IS_POST){
            $data = I('post.');
            $data['admin_id'] = session('admin_id');
            $distribut_obj = new DistributLogic();
            $res = $distribut_obj->updateRelation($id,$data);
            if($res){
                $this->success('更新成功',U('Distribut/relation'));
            }else{
                $this->error('更新失败',U('Distribut/relation'));
            }
        }
        $this->assign('relationRow',$relationRow);
        return $this->fetch();
    }


    /**
     * AJAX删除关系
     */
    public function delete_relation(){
        $id = I('id/d',0);
        if($id){
            $row = M('distribut_relation')->where(array('id'=>$id))->delete();
            if($row !== false){
                $this->ajaxReturn(array('status' => 1, 'msg' => '删除成功', 'data' => ''));
            }else{
                $this->ajaxReturn(array('status' => 0, 'msg' => '删除失败', 'data' => ''));
            }
        }else{
            $this->ajaxReturn(array('status' => 0, 'msg' => '参数错误', 'data' => ''));
        }
    }


    /*-----------------------小组管理------------------------*/

    /**
     * 小组首页
     * @return mixed
     */
    public function group(){
        return $this->fetch();
    }




    /**
     * AJAX获取数据
     * @return mixed
     */
    public function ajaxgroup()
    {
        // 搜索条件
        $condition = array();
        if (trim(I('keyword'))) {
            $condition['user_name'] = I('keyword');
        }
        $sort_order = I('order_by') . ' ' . I('sort');
        $model = M('distribut_big_group');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 15);
        //  搜索条件下 分页赋值
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $groupList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();
        $this->assign('groupList', $groupList);
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('pager', $Page);
        return $this->fetch();
    }



    /**
     * 小组详情
     */
    public function ajaxgroup_detail(){
        $id = I('id/d');
        //获取当前大组
        $bigGroup = M('distribut_big_group')->where(['id'=>$id])->find();
        //获取小组
        $smallGroup = M('distribut_small_group')->where(['bgid'=>$bigGroup['id']])->select();
        foreach ($smallGroup as &$rs) {
            //获取小组成员
            $str = 'sgid = '.$rs['id'].' and user_id <> '.$rs['user_id'];
            $rs['personList'] = M('distribut_group_person')->where($str)->select();
        }
        unset($rs);
        $this->assign('smallGroup',$smallGroup);
        $this->assign('bigGroup',$bigGroup);
        $this->assign('id',$id);
        return $this->fetch();
    }


    public function test(){

        for($i=1;$i<300;$i++){
            relationAlg($i);
        }
    }


}